package com.nvn.androidapp.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Environment;
import android.util.Log;

public class Statics {
	public static int SQUARE_MATRICS = 100;
	public static String MEDIA_STORAGE = Environment.getExternalStorageDirectory().getAbsolutePath()+"/AndroidApp/images";
	public static int CAMERA_REQUEST = 123;
	public static int REPLACE_PIC_REQUEST = 124;
	
	//convert DB to pixel
	public static int PixelFromDp(Context context,int dp){
		return (int)(dp*context.getResources().getDisplayMetrics().density);
	}
	public static int DpFromPixel(Context context,int pixel){
		return (int)(pixel/context.getResources().getDisplayMetrics().density);
	}
	
	public static Bitmap loadResizedBitmap(String filename, int width, int height, boolean exact ) {
	    Bitmap bitmap = BitmapFactory.decodeFile(filename);
	    if((bitmap.getWidth() - bitmap.getHeight()) * (width - height) < 0){
        	int tmp = width;
        	width = height;
        	height = tmp;
        }
	    
	    BitmapFactory.Options options = new BitmapFactory.Options();
	    options.inJustDecodeBounds = true;
	    BitmapFactory.decodeFile(filename, options );
	    
	    if ( options.outHeight > 0 && options.outWidth > 0 ) {
	        options.inJustDecodeBounds = false;
	        options.inSampleSize = 2;
	        options.inPreferredConfig = Bitmap.Config.RGB_565;
	        while (    options.outWidth  / options.inSampleSize > width
	                && options.outHeight / options.inSampleSize > height ) {
	            options.inSampleSize++;
	        }
	        options.inSampleSize--;

	        bitmap = BitmapFactory.decodeFile( filename, options );
	        
	        
	        
	        if(bitmap != null && bitmap.getWidth() > bitmap.getHeight() && bitmap.getWidth() > width){
	        	bitmap = Bitmap.createScaledBitmap( bitmap, width, (int)(bitmap.getHeight() / (bitmap.getWidth()/width)), false );
	        	if(bitmap.getHeight() > height){
	        		bitmap = Bitmap.createScaledBitmap( bitmap, (int)(bitmap.getWidth() / (bitmap.getHeight()/height)), height, false );
	        	}
	        }else if(bitmap != null && bitmap.getHeight() > bitmap.getWidth() && bitmap.getHeight() > height){
	        	bitmap = Bitmap.createScaledBitmap( bitmap, (int)(bitmap.getWidth() / (bitmap.getHeight()/height)), height, false );
	        }
	        
	        if ( bitmap != null && exact ) {
	            bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false );
	        }
	    }
	    if ( bitmap != null){
	    	Log.i("BITMAP SIZE",bitmap.getWidth()+" - "+bitmap.getHeight());
	    }
	    
	    
	    return bitmap;
	}
	
	public static Bitmap rotate(Bitmap bitmap, int degree) {
	    int w = bitmap.getWidth();
	    int h = bitmap.getHeight();

	    Matrix mtx = new Matrix();
	    mtx.postRotate(degree);

	    return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
	}
}
